#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash primer3-2.6.1.sh buildpath installpath "
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2

[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 

yum install -y  environment-modules time patch libatomic 

set -e
wget -t 0 -c -P $buildpath https://github.com/primer3-org/primer3/archive/refs/tags/v2.6.1.tar.gz
tar xf $buildpath/v2.6.1.tar.gz -C $buildpath && rm -rf $buildpath/v2.6.1.tar.gz

cd $buildpath/primer3-2.6.1/src

sed -i -e 's/gcc/clang/g' \
	-e 's/g++/clang++/g' Makefile
sed -i "62c CFLAGS  = $(CC_OPTS) $(O_OPTS) -march=armv8.2-a -fsigned-char -ffp-contract=off"  Makefile
sed -i "63c CXXFLAGS = $(CFLAGS) -std=c++11 -march=armv8.2-a -fsigned-char -ffp-contract=off"  Makefile
sed -i "69c PREFIX  = ${buildpath}/primer3-2.6.1"  Makefile

make
make install
cp -r $buildpath/primer3-2.6.1 $installpath

cat>"$installpath/primer3-2.6.1/primer_modulefiles"<<EOF
#%Module1.0
conflict primer
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set PRIMER \$pwd
setenv PRIMER \$PRIMER
prepend-path PATH \$PRIMER/bin
EOF


